package com.dyh.quickdemo.timelyPark;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;

/*
@Author=Joey Wheeler
@Date=2021/5/19 17:33
*/
@Slf4j
public class TimelyParkTest {



    @Test
    public void test(){

        Object[] res=new Object[1];
        log.info("开始:");
        ForkJoinPool.commonPool().submit(()->{
            try {
//                Thread.sleep(1000L);
                Thread.sleep(600L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("longTask finish");
            res[0]="1";//利用arr 修改值
        });

        LockSupport.parkNanos(1000*1000*500);
        if (res[0]==null)
            log.info("提前返回");
        else
            log.info("正常返回 {}",res[0]);

    }



    @Test
    public void test2(){
        Future<Integer> future = ForkJoinPool.commonPool().submit(()->{
            try {
//                Thread.sleep(1000L);
                Thread.sleep(600L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("longTask finish");
            return 1;
        });

        log.info("start");
        int res = 0;
        try {
            res =  future.get(500, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            /*
            java.util.concurrent.TimeoutException
	at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1065)

            */
            e.printStackTrace();
        }

        log.info("res={}",res);

    }
}
